package com.example.demo.algorithm.lesson.basic.class1;

/**
 * @author liweizhi
 * @date 2020/4/22 17:31
 * 在一个有序数组中，找>=某个数最左侧的位置
 */
public class BSNearLeft {
    public static int search(int[] arr, int n) {
        int none = -1;
        if (arr == null || arr.length == 0) {
            return none;
        }
        int left = 0;
        int right = arr.length - 1;
        int pre = none;
        while (left <= right) {
            int mid = left + ((right - left) >> 1);
            if (arr[mid] < n) {
                left = mid + 1;
            } else {
                pre = mid;
                right = mid - 1;
            }
        }
        return pre;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        System.out.println(search(arr, 1));
    }
}
